#include "itools.h"
#include "ilog.h"

void Java_com_ransj_iphone_utils_IScreenShot_readConfig(JNIEnv *env, jobject obj,
		jobject img, jint deepth, jint pixelformat) {
	/* see hardware/hardware.h */
	switch (pixelformat) {
	case 1: /* RGBA_8888 */
		LOG_D("1.....");
		setIntField(env, img, "bpp", 32);
		setIntField(env, img, "red_offset", 0);
		setIntField(env, img, "red_length", 8);
		setIntField(env, img, "green_offset", 8);
		setIntField(env, img, "green_length", 8);
		setIntField(env, img, "blue_offset", 16);
		setIntField(env, img, "blue_length", 8);
		setIntField(env, img, "alpha_offset", 24);
		setIntField(env, img, "alpha_length", 8);
		break;
	case 2: /* RGBX_8888 */
		LOG_D("2.....");
		setIntField(env, img, "bpp", 32);
		setIntField(env, img, "red_offset", 0);
		setIntField(env, img, "red_length", 8);
		setIntField(env, img, "green_offset", 8);
		setIntField(env, img, "green_length", 8);
		setIntField(env, img, "blue_offset", 16);
		setIntField(env, img, "blue_length", 8);
		setIntField(env, img, "alpha_offset", 24);
		setIntField(env, img, "alpha_length", 0);
		break;
	case 3: /* RGB_888 */
		LOG_D("3.....");
		setIntField(env, img, "bpp", 24);
		setIntField(env, img, "red_offset", 0);
		setIntField(env, img, "red_length", 8);
		setIntField(env, img, "green_offset", 8);
		setIntField(env, img, "green_length", 8);
		setIntField(env, img, "blue_offset", 16);
		setIntField(env, img, "blue_length", 8);
		setIntField(env, img, "alpha_offset", 24);
		setIntField(env, img, "alpha_length", 0);
		break;
	case 4: /* RGB_565 */
		LOG_D("4.....");
		setIntField(env, img, "bpp", 16);
		setIntField(env, img, "red_offset", 11);
		setIntField(env, img, "red_length", 5);
		setIntField(env, img, "green_offset", 5);
		setIntField(env, img, "green_length", 6);
		setIntField(env, img, "blue_offset", 0);
		setIntField(env, img, "blue_length", 5);
		setIntField(env, img, "alpha_offset", 0);
		setIntField(env, img, "alpha_length", 0);
		break;
	case 5: /* BGRA_8888 */
		LOG_D("5.....");
		setIntField(env, img, "bpp", 32);
		setIntField(env, img, "red_offset", 16);
		setIntField(env, img, "red_length", 8);
		setIntField(env, img, "green_offset", 8);
		setIntField(env, img, "green_length", 8);
		setIntField(env, img, "blue_offset", 0);
		setIntField(env, img, "blue_length", 8);
		setIntField(env, img, "alpha_offset", 24);
		setIntField(env, img, "alpha_length", 8);
		break;
	}
	jint width = getIntField(env, img, "width");
	jint height = getIntField(env, img, "height");
	if (width > height) {
		jint temp = width;
		width = height;
		height = temp;
		setIntField(env, img, "width", width);
		setIntField(env, img, "height", height);
	}
	LOG_D("width : %d, height : %d", width, height);
	setIntField(env, img, "size", width * height * deepth);
	jint t_width = width;
	if (width % 32) {
		t_width = width + 32 - (width % 32);
	}
	setIntField(env, img, "version", t_width - width);
	jsize size = t_width * height * deepth;
	setObjectField(env, img, "data", "[B", (*env)->NewByteArray(env, size));
}
